﻿var bll = {
    //页面加载初始化
    start: function () {
        $("#d_btn").css("display", "");
        $("#main").html("");
        $("input:radio:first").prop("checked", true);
        bll.comm.VictoriousSide = null;
        bll.comm.aNow1 = bll.comm.a1;
        bll.comm.aNow2 = bll.comm.a2;
        bll.comm.aNow3 = bll.comm.a3;
        bll.comm.huihe = 0;
    }
    //游戏开始
    , KaiJu: function (t) {
        bll.comm.indexModel = $(t).attr("value").substr(0, 2);
        while (bll.comm.VictoriousSide == null) {
            this.putong();
            if (bll.comm.indexModel == "红方") {
                bll.comm.indexModel = "蓝方"
            } else if (bll.comm.indexModel == "蓝方") {
                bll.comm.indexModel = "红方"
            }
        }
        $("#d_btn").css("display", "none");
    }
    //普通电脑随机数
    , suiji: function (t) {
        return Math.floor(Math.random() * t + 1);
    }
    , comm: {
        //篮子数量
        lanzi: 3
        //篮子最大数量
        , a1: 3
        , a2: 5
        , a3: 7
        //篮子当前数量
        , aNow1: 3
        , aNow2: 5
        , aNow3: 7
        //输赢方
        , VictoriousSide: null
        //回合数
        , huihe: 0
        //当前取的篮子编号
        , indexLanzi: 0
        //当前取的苹果编号
        , indexApple: 0
        //当前选手
        , indexModel: ""
        //当前操作结果输出文本
        , indexInputText: ""
        //绘制每回合的结果div
        , div: ""
        //绘制每回合的结果方法
        , LoadGamePage: function () {
            bll.comm.div = '<div class="d-parent">';
            this.LoadImg(this.aNow1, this.aNow2, this.aNow3);
            this.LoadText(this.huihe, this.aNow1, this.aNow2, this.aNow3);
            bll.comm.div += '</div>';
            $("#main").append(bll.comm.div);
        }
        , LoadImg: function (a1, a2, a3) {
            bll.comm.div += '<div class="d">';
            this.LoadImg1(a1);
            bll.comm.div += '</div>';
            bll.comm.div += '<div class="d d2">';
            this.LoadImg1(a2);
            bll.comm.div += '</div>';
            bll.comm.div += '<div class="d d3">';
            this.LoadImg1(a3);
            bll.comm.div += '</div>';
        }
        , LoadImg1: function (t) {
            for (var i = 0; i < t; i++) {
                bll.comm.div += '<img src="/img/a.jpg" />';
            }
        }
        , LoadText: function (t, a1, a2, a3) {
            if (t == 0) return;
            bll.comm.div += '第' + t + '回合开始！<br><br>';
            bll.comm.div += bll.comm.indexInputText;
            if (bll.comm.lanziCount == 0) {
                bll.comm.div += '<br>' + bll.comm.VictoriousSide + '胜利！';
            }
        }
    }
    , putong: function () {
        bll.comm.lanziCount = bll.lanziCount();
        if (bll.comm.lanziCount == 0) {
            bll.comm.VictoriousSide = bll.comm.indexModel;
            bll.comm.huihe++;
            bll.comm.LoadGamePage();
            alert("游戏结束【" + bll.comm.VictoriousSide + "】赢了！！！");
            return;
        }
        //得到当前取的篮子编号comm.indexLanzi
        while (bll.comm.indexLanzi == 0) {
            if (bll.comm.lanziCount == 1) {
                break;
            }
            var index = this.suiji(3);
            if (index == 1 && bll.comm.aNow1 > 0) {
                this.comm.indexLanzi = index;
            }
            if (index == 2 && bll.comm.aNow2 > 0) {
                this.comm.indexLanzi = index;
            }
            if (index == 3 && bll.comm.aNow3 > 0) {
                this.comm.indexLanzi = index;
            }
        }
        //得到当前取的篮子抓取苹果数量
        var index = this.suiji(bll.appleCount(bll.comm.indexLanzi));
        //计算剩余量
        this.shengyuApple(index, bll.comm.indexLanzi);
        if (bll.comm.indexModel == "红方") {
            bll.comm.indexInputText += '红方';
        } else {
            bll.comm.indexInputText += '蓝方';
        }
        bll.comm.indexInputText += '抓取' + bll.comm.indexLanzi + '号篮子中的' + index + '个苹果。目前：1号篮子剩余' + bll.comm.aNow1 + '个苹果，2号篮子剩余' + bll.comm.aNow2
            + '个苹果，3号篮子剩余' + bll.comm.aNow3 + '个苹果。<br>';

        //绘制结果
        if (bll.comm.indexInputText.indexOf('红方') > -1 && bll.comm.indexInputText.indexOf('蓝方') > -1) {
            bll.comm.huihe++;
            bll.comm.LoadGamePage();
            bll.comm.indexInputText = "";
        }

        //重置下回合参数
        bll.comm.indexLanzi = 0;
        bll.comm.indexApple = 0;
    }
    //当前篮子的数量
    , lanziCount: function () {
        var i = 0;
        var lanzi = 0;
        if (bll.comm.aNow1 > 0) {
            i++;
            lanzi = 1;
        }
        if (bll.comm.aNow2 > 0) {
            i++;
            lanzi = 2;
        }
        if (bll.comm.aNow3 > 0) {
            i++;
            lanzi = 3;
        }
        if (i == 1) {
            bll.comm.indexLanzi = lanzi;
        }
        return i;
    }
    //当前篮子的苹果数量
    , appleCount: function (t) {
        switch (t) {
            case 1:
                return bll.comm.aNow1;
                break;
            case 2:
                return bll.comm.aNow2;
                break;
            case 3:
                return bll.comm.aNow3;
                break;
            default:
        }
    }
    //计算剩余苹果
    , shengyuApple: function (i, a) {
        switch (a) {
            case 1:
                bll.comm.aNow1 = bll.comm.aNow1 - i;
                break;
            case 2:
                bll.comm.aNow2 = bll.comm.aNow2 - i;
                break;
            case 3:
                bll.comm.aNow3 = bll.comm.aNow3 - i;
                break;
            default:
        }
    }
};